package com.bifrost.service.impl;

import com.bifrost.service.JedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Set;

/**
 * @author rangf
 * @date 2018/3/30
 */
@Service
public class JedisServiceImpl implements JedisService {

    @Autowired
    JedisPool jedisPool;
    @Autowired
    RedisTemplate redisTemplate;

    //字符串操作
    public void set(String key, String value){
        try(Jedis jedis = jedisPool.getResource()) {
            jedis.set(key, value);
        }catch (Exception e){
            throw new RuntimeException("redis client exception");
        }
    }

    public String get(String key) {
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.get(key);
        } catch (Exception e) {
            throw new RuntimeException("redis client exception");
        }
    }

    public void del(String key) {
        try (Jedis jedis = jedisPool.getResource()) {
             jedis.del(key);
        } catch (Exception e) {
            throw new RuntimeException("redis client exception");
        }
    }
    //set 操作
    public Long sadd(String key, String value){
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.sadd(key, value);
        } catch (Exception e) {
            throw new RuntimeException("redis client exception");
        }
    }

    public Set<String> smembers(String key){
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.smembers(key);
        } catch (Exception e) {
            throw new RuntimeException("redis client exception");
        }
    }

}
